package guestbook;

import java.io.BufferedReader;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.*;

import org.mortbay.log.Log;

import guestbook.Message;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.appengine.labs.repackaged.org.json.JSONArray;
import com.google.appengine.labs.repackaged.org.json.JSONObject;

public class GBBServlet extends HttpServlet {
	int msg_id_lsc=0,msg_id_xmg=0;
	private ArrayList<Message> al_lsc=new ArrayList<Message>(); 
	private ArrayList<Message> al_xmg=new ArrayList<Message>(); 
	
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
              throws IOException {
    	
        /*String msg = req.getParameter("msg");
    	String SourceIp = "";
    	
    	if (req.getHeader("x-forwarded-for") == null) { 
		    SourceIp = req.getRemoteAddr(); 
		  } 
    	SourceIp = req.getHeader("x-forwarded-for"); 
		
		  resp.setContentType("text/plain");
		  resp.getWriter().println(SourceIp);
    	msg_id_lsc=0;
    	msg_id_xmg=0;
    	al_lsc.clear();
    	al_xmg.clear();*/
    	List<Message> lm = null;
    	lm = new AddMsg().list();
    	
    	Message m1 = lm.get(0);
    	//Log.debug(m1.get_msg());
    	
    	int remote_msg_ID = Integer.valueOf(req.getParameter("remote_msg_ID"));
    	String username = req.getParameter("username");
    	
    	if(username.equalsIgnoreCase("LSC"))
    	{
    		String content = "";
    		JSONObject json=new JSONObject();
    		JSONArray jsa = new JSONArray();
    		
    		try{
    		int i =remote_msg_ID+1;
    		//int i =0;
    		for(;i<=msg_id_xmg;i++)
    		{
    			JSONObject member1 = new JSONObject();
    	        member1.put("remote_msg_ID", String.valueOf(i));
    	        member1.put("msg", ((Message) al_xmg.get(i-1)).get_msg());
    	        member1.put("username", "XMG");
    	        jsa.put(member1);
    		}
            json.put("msgs", jsa);
            }catch(Exception e){}
            content = json.toString();
    		  resp.setContentType("text/plain;charset=utf-8");
    		  //resp.getWriter().println("HelloPOST, source ip is "+ SourceIp + "\nmsg_ID is "+ msg_id +"\ncontent is " + content);
    		  resp.getWriter().println(content);
    			
        	
    	
    	}
    	else{
    		String content = "";
			JSONObject json=new JSONObject();
			JSONArray jsa = new JSONArray();
			
			try{
			int i =Integer.valueOf(remote_msg_ID)+1;
			for(;i<=msg_id_lsc;i++)
			{
				JSONObject member1 = new JSONObject();
		        member1.put("remote_msg_ID", String.valueOf(i));
		        member1.put("msg", "msg: "+ ((Message) al_lsc.get(i-1)).get_msg());
		        member1.put("username", "LSC");
		        jsa.put(member1);
			}
	        json.put("msgs", jsa);
	        }catch(Exception e){}
	        content = json.toString();
			resp.setContentType("text/plain;charset=utf-8");
			  //resp.getWriter().println("HelloPOST, source ip is "+ SourceIp + "\nmsg_ID is "+ msg_id +"\ncontent is " + content);
			resp.getWriter().println(content);
			
    	}
    	
    }
    
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
		    throws IOException {
    	
    	int msg_id = Integer.valueOf(req.getParameter("msg_ID"));
    	int remote_msg_ID = Integer.valueOf(req.getParameter("remote_msg_ID"));
    	String username = req.getParameter("username");
    	String msg = req.getParameter("msg");
    	Calendar nowc=Calendar.getInstance();
    	String now = String.valueOf(nowc.get(Calendar.DAY_OF_MONTH));
    	Message m = new Message(now,msg_id,username,msg);
    	
    	if(username.equalsIgnoreCase("LSC"))
    	{
    		msg_id_lsc++;
    		al_lsc.add(m);
    		new AddMsg().addmsg(m);
    		
    		String content = "";
    		JSONObject json=new JSONObject();
    		JSONArray jsa = new JSONArray();
    		
    		try{
    		int i =Integer.valueOf(remote_msg_ID)+1;
    		//int i =0;
    		for(;i<=msg_id_xmg;i++)
    		{
    			JSONObject member1 = new JSONObject();
    	        member1.put("remote_msg_ID", String.valueOf(i));
    	        member1.put("msg", ((Message) al_xmg.get(i-1)).get_msg());
    	        member1.put("username", "XMG");
    	        jsa.put(member1);
    		}
            json.put("msgs", jsa);
            }catch(Exception e){}
            content = json.toString();
    		  resp.setContentType("text/plain;charset=utf-8");
    		  //resp.getWriter().println("HelloPOST, source ip is "+ SourceIp + "\nmsg_ID is "+ msg_id +"\ncontent is " + content);
    		  resp.getWriter().println(content);
    			
        	
    	
    	}
    	else{
    		al_xmg.add(m);
    		new AddMsg().addmsg(m);
    		msg_id_xmg ++;
    		String content = "";
			JSONObject json=new JSONObject();
			JSONArray jsa = new JSONArray();
			
			try{
			int i =Integer.valueOf(remote_msg_ID)+1;
			for(;i<=msg_id_lsc;i++)
			{
				JSONObject member1 = new JSONObject();
		        member1.put("remote_msg_ID", String.valueOf(i));
		        member1.put("msg", "msg: "+ ((Message) al_lsc.get(i-1)).get_msg());
		        member1.put("username", "LSC");
		        jsa.put(member1);
			}
	        json.put("msgs", jsa);
	        }catch(Exception e){}
	        content = json.toString();
			resp.setContentType("text/plain;charset=utf-8");
			  //resp.getWriter().println("HelloPOST, source ip is "+ SourceIp + "\nmsg_ID is "+ msg_id +"\ncontent is " + content);
			resp.getWriter().println(content);
			
    	}
    }
    
	}
